﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using EShop.Core;
using EShop.Core.Caching;
using EShop.Core.Data;
using EShop.Core.Events;
using EShop.Core.Model.Store;

namespace EShop.Services.Store
{
    public class DimensionValueService : IDimensionValueService
    {
        #region Constants
        private const string DIMENSIONVALUE_BY_ID_KEY = "DimensionValue.id-{0}";
        private const string DIMENSIONVALUE_PATTERN_KEY = "DimensionValue";
        #endregion

        #region Fields
        private readonly IRepository<DimensionValue> _dimensionValueRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        //private readonly IDbContext _context;
        #endregion

        #region Ctor
        public DimensionValueService(IRepository<DimensionValue> dimensionValueRepository, ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            _dimensionValueRepository = dimensionValueRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
        }
        #endregion

        public DimensionValue GetDimensionValueById(string id)
        {
            if (string.IsNullOrEmpty(id))
                return null;
            //格式化缓存条件
            string key = string.Format(DIMENSIONVALUE_BY_ID_KEY, id);
            return _cacheManager.Get(key, () =>
            {
                var dimensionValue = _dimensionValueRepository.GetById(id);
                return dimensionValue;
            });
        }

        public bool AddDimensionValue(DimensionValue model)
        {
            if (model == null) throw new ExtException("NUll DimensionValue Model");

            int rs = _dimensionValueRepository.Insert(model);
            if (rs > 0)
            {
                //event notification
                _eventPublisher.EntityInserted(model);

            }
            return rs > 0 ? true : false; ;
        }

        public bool UpdateDimensionValue(DimensionValue model)
        {
            if (model == null) throw new ExtException("NUll DimensionValue Model");

            int rs = _dimensionValueRepository.Update(model);
            if (rs > 0)
            {
                //event notification
                _eventPublisher.EntityInserted(model);

            }
            return rs > 0 ? true : false;
        }

        public bool DelDimensionValue(string id)
        {
            throw new NotImplementedException();
        }

        public IList<DimensionValue> GetAllDimensionValue()
        {
            var dimensionValue = _dimensionValueRepository.Table.ToList();
            return dimensionValue;
        }

        public IList<DimensionValue> GetDimensionValueByDimensionId(int dimensionId)
        {
            var dimensionValue = _dimensionValueRepository.Table.Where(c => c.DimensionId == dimensionId).ToList();
            return dimensionValue;
        }
    }
}
